package com.shundr.shipper.common.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.common.http.CommonRequest;
import com.shundr.shipper.common.model.RecomCargoInfo;
import com.shundr.shipper.common.util.CommonConfig;
import com.shundr.shipper.common.util.DateUtil;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.util.StringUtil;
import com.shundr.shipper.truck.ContactTrucker;
import com.shundr.shipper.truck.ContactTrucker.OnCallListener;
import com.shundr.shipper.user.LoginActivity;

public class RecomCargoAdapter extends BaseAdapter {
	public List<RecomCargoInfo> mList;
	private Context mContext;

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case CommonRequest.ADD_FAVORITE_SUCCEED:
				try {
					ToastHelper.toastShort(mContext, msg.obj.toString());
//					RecomCargoInfo info = mList.get(mPosition);
//					info.setFavorited(true);
//					notifyDataSetChanged();
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			case LHttpRequest.REQUEST_FAILED:
				try {
					ToastHelper.toastShort(mContext, msg.obj.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};
	

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int index) {
		return mList.get(index);
	}

	@Override
	public long getItemId(int id) {
		return id;
	}

	public RecomCargoAdapter(Context mContext, List<RecomCargoInfo> list) {
		super();
		this.mList = list;
		this.mContext = mContext;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup viewGroup) {
		ViewHold viewHold = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_recom_cargo_list, null);
			viewHold = new ViewHold();
			viewHold.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			viewHold.tvDes = (TextView) convertView.findViewById(R.id.tv_des);
			viewHold.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
			viewHold.layoutCollection = (LinearLayout) convertView.findViewById(R.id.layout_collect);
			viewHold.layoutCall = (LinearLayout) convertView.findViewById(R.id.layout_call);
			viewHold.ivFavoriteStatus=(ImageView)convertView.findViewById(R.id.iv_favorite_status);
			viewHold.tvCollect=(TextView)convertView.findViewById(R.id.tv_collect_status);
			convertView.setTag(viewHold);
		} else {
			viewHold = (ViewHold) convertView.getTag();
		}
		try {
			final RecomCargoInfo info = mList.get(position);
			viewHold.tvTitle.setText(info.getRecommendTitle());
			viewHold.tvDes.setText(info.getRecommendMsg());

			// viewHold.tvTime.setText(DateUtil.getDividerDate(info.getRecommendTime()));
			viewHold.tvTime.setText(DateUtil.getInterval(info
					.getRecommendTime()));
			viewHold.layoutCollection.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (LContext.isLogin()) {
						if(info.isFavorited()){
							ToastHelper.toastShort(mContext, "您已经收藏了此货源");
							return;
						}
						// 收藏货源
						new CommonRequest(mContext, mHandler).addFavorite(
								info.getCargoId(),
								CommonConfig.OPERATE_OBJECT_CARGO);
					} else {
						mContext.startActivity(new Intent(mContext, LoginActivity.class));
					}
				}
			});
			
			if(info.isFavorited()){
				viewHold.tvCollect.setText("已收藏");
				viewHold.ivFavoriteStatus.setBackgroundResource(R.drawable.iv_favorite_yes);
			}else{
				viewHold.tvCollect.setText("收藏货源");
				viewHold.ivFavoriteStatus.setBackgroundResource(R.drawable.iv_favorite_no);
			}
			viewHold.layoutCall.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (LContext.isLogin()) {
						new CommonRequest(mContext, new Handler())
								.saveOperateHistory(info.getCargoId(),
										CommonConfig.OPERATE_OBJECT_CARGO,
										CommonConfig.OPERATE_CALL, "");
						if (LContext.getUserValidationStatus() != -1) {//认证完
							OnCallPhone(info);
						}else{//未认证通过
							ContactTrucker trucker = new ContactTrucker(mContext);
							trucker.setCallListener(new OnCallListener() {
								
								@Override
								public void onCall() {
									OnCallPhone(info);
								}
							});
						}
					} else {
						mContext.startActivity(new Intent(mContext,
								LoginActivity.class));
					}
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	private void OnCallPhone(RecomCargoInfo info) {
		// if (!StringUtil.isNull(info.getCargoCompanyPhone1())) {
		if (!StringUtil.isNull("")) {
			String mobileText = "".replace("-", "");
			Intent intent = new Intent();// 创建一个意图对象，用来激发拨号的Activity
			intent.setAction("android.intent.action.CALL");
			intent.setData(Uri.parse("tel:" + mobileText));
			mContext.startActivity(intent);// 方法内部会自动添加类别,android.intent.category.DEFAULT
		} else {
			ToastHelper.toastShort(mContext, "该货主未提供联系电话");
		}
	}

	static class ViewHold {
		TextView tvTitle;
		TextView tvDes;
		TextView tvTime;

		TextView tvCollect;

		ImageView ivFavoriteStatus;

		LinearLayout layoutCollection;
		LinearLayout layoutCall;
	}
}
